﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ezCloud.Data;
using Microsoft.AspNet.Identity;
using ezCloud.Api.Models;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using ezCloud.Api.Common;
using System.Data.SqlTypes;
using System.Dynamic;
using System.Data.Linq.SqlClient;
using System.Text.RegularExpressions;

namespace ezCloud.Api.Controllers
{
    [RoutePrefix("api/HouseKeeping")]
    public class HouseKeepingController : ezController
    {
        public HouseKeepingController()
        {

        }
        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("HouseStatus")]
        public object getHouseStatus()
        {
            var user = Common.UserUtitily.getUserProfile();
            
            var rooms = context.Rooms.Where(rm => rm.HotelId == user.HotelId && (rm.HouseStatus != "REPAIR" || rm.HouseStatus == null) && rm.IsHidden == false && rm.IsDeleted==false)
                .Select(rm => new
                {
                    rm.HotelId,
                    rm.RoomId,
                    rm.RoomName,
                    rm.RoomLocation,
                    rm.RoomDescription,
                    rm.HouseStatus,
                    rm.IsHidden,
                    rm.RepairStartDate,
                    rm.RepairEndDate,
                    rm.RoomTypes
                }).ToList();
            //List<RoomTypes> roomTypes = context.RoomTypes.Where(rt => rt.HotelId == user.HotelId).ToList();
            /*List<ReservationRooms> reservationRooms = context.ReservationRooms.Where(rr => rr.HotelId == user.HotelId).ToList();
            reservationRooms.GroupBy(rr => rr.Rooms.RoomId)
                .Select(grp => grp.ToList())
                .ToList();
            foreach(var reservation in reservationRooms)
            {
                reservation
            }*/
            return rooms;
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("ProcessHouseStatus")]
        public async Task<IHttpActionResult> processHouseStatus(List<RoomSelected> roomList)
        {
            var user = Common.UserUtitily.getUserProfile();
            
            /*foreach (var room in roomList)
            {
                Rooms currentRoom = context.Rooms.Where(rm => rm.HotelId == room.HotelId && rm.RoomId == room.RoomId).FirstOrDefault();
                currentRoom.HouseStatus = null;
                context.SubmitChanges();
                Common.Elastic.IndexLastAudit(context);
            }*/
            for (int i = 0; i < roomList.Count; i++)
            {
                Rooms currentRoom = context.Rooms.Where(rm => rm.HotelId == roomList[i].HotelId && rm.RoomId == roomList[i].RoomId).FirstOrDefault();
                if (currentRoom != null && currentRoom.HouseStatus != "REPAIR")
                {
                    //currentRoom.HouseStatus = null;
                    // context.SubmitChanges();
                    currentRoom.HouseStatus = roomList[i].HouseStatus;
                }
            }
            
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            //Common.MemoryCacher.ClearStatusCache(user.HotelId);
            return Ok();
        }
        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("ProcessHouseStatusForOnlyRoom")]
        public async Task<IHttpActionResult> ProcessHouseStatusForOnlyRoom(int roomId)
        {
            var user = Common.UserUtitily.getUserProfile();
            

            Rooms currentRoom = context.Rooms.Where(rm => rm.HotelId == user.HotelId && rm.RoomId == roomId).FirstOrDefault();
            if (currentRoom != null && currentRoom.HouseStatus != "REPAIR")
            {

                currentRoom.HouseStatus = "DIRTY";
                context.SubmitChanges();
                Common.Elastic.IndexLastAudit(context);
            }
            
            return Ok();
        }
    }
}